package com.haha.gulimallsearch.controller;

import com.haha.common.exception.BizCodeEnum;
import com.haha.common.to.es.SkuEsModel;
import com.haha.common.utils.R;
import com.haha.gulimallsearch.service.ProductSaveService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/search/save")
@Log4j
public class ElasticSaveController {

    @Autowired
    private ProductSaveService productSaveService;

    @PostMapping("/product")
    public R productSave(@RequestBody List<SkuEsModel> skuEsModels)  {
        boolean b = false;
        try {
            b = productSaveService.productStatusUp(skuEsModels);

        } catch (IOException e) {
            log.error("商品上架错误");
           return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }

      if(b){
          return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
      }
      else{
          return R.ok();
      }
    }

}
